package cutetd.action;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.opensymphony.xwork2.ActionSupport;

import cutetd.dao.GoodsDao;
import cutetd.dao.NaviDao;
import cutetd.model.*;
import cutetd.vo.Page;
import cutetd.vo.PageNavi;

/**
 * @author flreey
 * 
 */
public class GoodsAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<M_Navi> naviList = new ArrayList<M_Navi>();
	private List<M_Goods> goodsList = new ArrayList<M_Goods>();
	private GoodsDao goodsDao = new GoodsDao();
	private NaviDao naviDao = new NaviDao();
	private M_Goods goods;
	private String id;
	private String message;
	
	private String naviKey;

	// 控制分页属性
	private int curPage = 1;
	private int totalPage;
	private List<Page> pageList = new ArrayList<Page>();
	private PageNavi pageNavi = new PageNavi(M_Goods.class, 2, 5);

	@SuppressWarnings("unchecked")
	public String manager() {
		pageList = pageNavi.getPageList(curPage);
		setGoodsList((List<M_Goods>)pageNavi.getObjectList(curPage, "likeNo >= 0", "likeNo asc"));
		setNaviList(naviDao.getListAll());
		setTotalPage(pageNavi.getTotalPages());
		
		return SUCCESS;
	}


	@SuppressWarnings("unchecked")
	public String managerByType() {
		pageList = pageNavi.getPageList(curPage);
		//System.out.println(naviKey);

		setGoodsList((List<M_Goods>)pageNavi.getObjectList(curPage, "naviKey==\"" + naviKey + "\"", "likeNo asc"));
		setNaviList(naviDao.getListAll());
		setTotalPage(pageNavi.getTotalPages());
		return SUCCESS;
	}
	
	public String toAdd() {
		setNaviList(getListByType("goods"));
		return "toAdd";
	}

	private List<M_Navi> getListByType(String type) {
		List<M_Navi> list = naviDao.getListAll();
		List<M_Navi> temp = new ArrayList<M_Navi>();

		for (int i = 0; i < list.size(); ++i) {
			if (list.get(i).getType().equals(type)) {
				temp.add(list.get(i));
			}
		}

		return temp;
	}

	public String toUpdate() {
		goods = goodsDao.getObjectByKey(id);
		return "toUp";
	}

	public String addGoods() {
		setNaviList(getListByType("goods"));
		
		goods.setId(UUID.randomUUID().toString());
		goodsDao.save(goods);
		setMessage("添加成功!");
		
		
		return "toAdd";
	}

	public String updateGoods() {
		goodsDao.update(goods);
		setGoodsList(goodsDao.getListAll());
		setNaviList(naviDao.getListAll());
		return "manager";
	}

	public String deleteGoods() {
		// System.out.println(goods.getId());
		goodsDao.delete(id);
		setGoodsList(goodsDao.getListAll());
		setNaviList(naviDao.getListAll());
		return "manager";
	}

	public List<M_Navi> getNaviList() {
		return naviList;
	}

	public void setNaviList(List<M_Navi> naviList) {
		this.naviList = naviList;
	}

	public M_Goods getGoods() {
		return goods;
	}

	public void setGoods(M_Goods goods) {
		this.goods = goods;
	}

	public List<M_Goods> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<M_Goods> goodsList) {
		this.goodsList = goodsList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public double getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public List<Page> getPageList() {
		return pageList;
	}

	public void setPageList(List<Page> pageList) {
		this.pageList = pageList;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	public String getNaviKey() {
		return naviKey;
	}


	public void setNaviKey(String naviKey) {
		this.naviKey = naviKey;
	}
}
